package com.eduction.common;

public class UnicodeUtil {
    public static String decodeUnicode(final String dataStr) {
        int start = 0;
        int end = 0;
        final StringBuffer buffer = new StringBuffer();
        while (start > -1) {
            end = dataStr.indexOf("\\u", start + 2);
            String charStr = "";
            if (end == -1) {
                charStr = dataStr.substring(start + 2, dataStr.length());
            } else {
                charStr = dataStr.substring(start + 2, end);
            }
            char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
            buffer.append(new Character(letter).toString());
            start = end;
        }
        return buffer.toString();
    }

    public static String stringtoUnicode(String string) {
        if(string==null||"".equals(string)){
            return null;
        }
        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            unicode.append("\\u" + Integer.toHexString(c));
        }
        return unicode.toString();
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s = stringtoUnicode("中文");
        System.out.println("编码："+s);
        String s1 = decodeUnicode("%E4%BC%98%E7%A7%80");
        System.out.println("解码："+s1);

    }

}
